package the.workshop.bu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.StringUtils;
import org.springframework.stereotype.Service;
import org.wisteria.framework.web.BusinessException;
import the.workshop.bu.constant.CheckBizTypeEnum;
import the.workshop.bu.constant.CheckTypeEnum;
import the.workshop.bu.dto.*;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.entity.CustomerTel;
import the.workshop.bu.service.ICheckService;
import the.workshop.bu.service.ICustomerBizCheckService;
import the.workshop.bu.service.ICustomerService;
import the.workshop.bu.service.ICustomerTelService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CustomerBizCheckServiceImpl implements ICustomerBizCheckService {

    @Resource
    private ICustomerService customerService;

    @Resource
    private ICustomerTelService customerTelService;

    @Resource
    private ICheckService checkService;

    @Override
    public CheckDataDTO checkTelData(CheckTelQueryDTO query) {
        int dealTotal = 0;
        Map<String, Object> checkData = null;
        List<CheckTelDTO> telList = customerTelService.listCheckTel(query);
        int total = telList.size();
        for (CheckTelDTO tel : telList) {
            boolean isRemove = false;
            String mobile = tel.getTelNumber();
            if (!StringUtils.hasText(mobile) || mobile.length() != 11) {
                isRemove = true;
                continue;
            }

            checkData = new HashMap<>();
            checkData.put("customerId", tel.getCustomerId());
            checkData.put("customerName", tel.getCustomerName());
            checkData.put("mobile", tel.getTelNumber());

            if (CheckTypeEnum.MOBILE_EMPTY.getValue() == query.getCheckType()) {
                ServiceResult<MobileEmptyDTO> emptyResult = checkService.checkMobileEmpty(CheckBizTypeEnum.CUSTOMER, tel.getId(), mobile, checkData);
                if (!emptyResult.getSuccess()) {
                    log.error("空号检测失败, Data: " + checkData.toString() + ", Result: " + emptyResult.toString());
                    throw BusinessException.withErrorCode("空号检测失败，" + emptyResult.getMsg());
                }
                MobileEmptyDTO mobileEmpty = (MobileEmptyDTO) emptyResult.getData();
                if (mobileEmpty.getStatus().intValue() == 0 || mobileEmpty.getStatus().intValue() == 3) {
                    isRemove = true;
                }
            } else if (CheckTypeEnum.MOBILE_NAME.getValue() == query.getCheckType()) {
                if (!StringUtils.hasText(tel.getLegal())) {
                    continue;
                }

                checkData.put("legal", tel.getLegal());
                ServiceResult<MobileNameDTO> mobileNameResult = checkService.checkMobileAndName(CheckBizTypeEnum.CUSTOMER, tel.getId(), mobile, tel.getLegal(), checkData);
                if (!mobileNameResult.getSuccess()) {
                    log.error("二要素检测失败, Data: " + checkData.toString() + ", Result: " + mobileNameResult.toString());
                    throw BusinessException.withErrorCode("二要素检测失败，" + mobileNameResult.getMsg());
                }

                if (mobileNameResult.getData() != null && mobileNameResult.getData().getResult().intValue() == 0) {
                    setLegalTel(tel.getId());
                    setLegalStatus(tel.getCustomerId());
                    dealTotal++;
                }
            }

            if (isRemove) {
                customerTelService.removeById(tel.getId());
            }
        }
        return new CheckDataDTO(total, dealTotal);
    }

    private void setLegalTel(int id) {
        LambdaUpdateWrapper<CustomerTel> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(CustomerTel::getIsLegal, 1)
                  .set(CustomerTel::getModifiedTime, new Date())
                .eq(CustomerTel::getId, id)
                .eq(CustomerTel::getIsDeleted, 0);
        customerTelService.update(updateWrapper);
    }

    private void setLegalStatus(int customerId) {
        LambdaUpdateWrapper<Customer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Customer::getStatus, "01001")
                .set(Customer::getModifiedTime, new Date())
                .set(Customer::getStatusTime, new Date())
                .eq(Customer::getId, customerId)
                .eq(Customer::getIsDeleted, 0);
        customerService.update(updateWrapper);
    }

    public static void main(String[] args) {
        int pageToal = 2 % 3;
        System.out.println(pageToal);
    }
}
